package com.example.tian.securityinspection.base;

import android.util.Log;


import com.example.tian.securityinspection.context.AppConfig;
import com.example.tian.securityinspection.model.bean.ResponseError;
import com.example.tian.securityinspection.ui.Display;
import com.google.common.base.Preconditions;

import java.net.FileNameMap;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

public abstract class BaseController<U extends BaseController.Ui<UC>,UC>{

    private final Set<U> mUis;
    private final Set<U> mUnmodifiableUis;

    private Display mDisplay;
    private boolean mInited;

    /*设置显示属性*/
    public BaseController(){
        mUis = new CopyOnWriteArraySet<>();
        mUnmodifiableUis = Collections.unmodifiableSet(mUis);
    }

    /*初始化*/
    public final void init(){
        Preconditions.checkState(!mInited, "Already inited");
        onInited();
        mInited = true;
    }

    /*延迟处理*/
    public final void suspend(){
        Preconditions.checkState(mInited, "Not inited");
        onSuspended();
        mInited = false;
    }

    protected void onInited(){}

    protected void onSuspended(){}

    public final boolean isInited(){
        return mInited;
    }

    /*初始化UI的抽象方法*/
    protected abstract UC createUiCallbacks(U ui);

    /*为UI添加附加属性*/
    public synchronized final void attachUi(U ui){
        Preconditions.checkArgument(ui != null, "Ui cannot be null");
        Preconditions.checkState(!mUis.contains(ui), "Ui is already attached");
        mUis.add(ui);
        ui.setCallbacks(createUiCallbacks(ui));
    }

    /*各种生命周期*/
    public synchronized final void startUi(U ui){
        Preconditions.checkArgument(ui != null, "Ui cannont be null");
        Preconditions.checkState(mUis.contains(ui), "ui is not attached");
        populateUi(ui);
    }

    public synchronized final void detachUi(U ui){
        Preconditions.checkArgument(ui != null, "ui cannot be null");
        Preconditions.checkState(mUis.contains(ui), "ui is not attached");
        ui.setCallbacks(null);
        mUis.remove(ui);
    }

    /*构筑ui*/
    protected synchronized void populateUi(U ui){}

    protected synchronized final void populateUis(){
        if (AppConfig.DEBUG){
            Log.d(getClass().getSimpleName(), "populateUis");
        }
        for (U ui : mUis){
            populateUi(ui);
        }
    }

    /*获取Ui的方法*/
    protected final Set<U> getUis(){
        return mUnmodifiableUis;
    }

    /*获取Ui的id*/
    protected int getId(U ui){
        return ui.hashCode();
    }

    /*通过Ui的id来得到相应Ui*/
    protected synchronized U findUi(final int id){
        for (U ui : mUis){
            if (getId(ui) == id){
                return ui;
            }
        }
        return null;
    }

    /*设置显示器*/
    public void setDisplay(Display display){
        mDisplay = display;
    }

    /*获取显示器*/
    public final Display getDisplay(){
        return mDisplay;
    }

    /*UI 数据传递的接口*/
    public interface Ui<UC>{
        void setCallbacks(UC callbacks);

        UC getCallbacks();

        void onResponseError(ResponseError error);
    }

    public interface ListUi<T, UC> extends Ui<UC>{
        void onStartRequest(int page);

        void onFinishRequest(List<T>items, int page, boolean haveNextPage);
    }

    public interface SubUi{}

}
